package com.swz.dcrm.ui.car;


import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.View;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.swz.dcrm.adpter.CarModelAdapter;
import com.swz.dcrm.adpter.GlobalAdapter;
import com.swz.dcrm.model.CarModel;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.navigation.fragment.NavHostFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;

import com.swz.dcrm.ui.viewmodel.CarBrandViewModel;
import com.xh.baselibrary.callback.OnClickListener;

import javax.inject.Inject;

/**
 * A simple {@link Fragment} subclass.
 */
public class CarModelFragment extends BaseFragment {

    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;

    private int page=1;
    private int size=40;
    private long total;

    private long seriesId;
    @Inject
    CarBrandViewModel carBrandViewModel;
    private CarModelAdapter carModelAdapter;
    public static CarModelFragment newInstance() {

        return new CarModelFragment();
        // Required empty public constructor
    }

    @Override
    public void onLoadRetry() {
        if (!isNetworkConnected(getContext())){
            mHolder.showLoadingStatus(GlobalAdapter.NET_ERROR);
            return;
        }
        carBrandViewModel.getCarModels(page,size,seriesId)
                .observe(this,pageBaseResponse -> {
                    smartRefreshLayout.finishLoadmore();
                    smartRefreshLayout.finishRefresh();
                    if(pageBaseResponse.isSuccess()){
                        total=pageBaseResponse.getTotal();
                        if(carModelAdapter==null){
                            carModelAdapter=new CarModelAdapter(getContext(),pageBaseResponse.getData());
                            carModelAdapter.setOnClickListener(onClickListener);
                            rv.setAdapter(carModelAdapter);
                        }else {
                            carModelAdapter.refresh(pageBaseResponse.getPage(),pageBaseResponse.getData());
                        }

                    }
                });

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onLoadRetry();
    }


    @Override
    public int layoutId() {
        return R.layout.fragment_car_model;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        seriesId=Long.valueOf(CarModelFragmentArgs.fromBundle(getArguments()).getSeriesId());
        smartRefreshLayout.setEnableRefresh(false);
        rv.setLayoutManager(new LinearLayoutManager(getContext()));
        title.setText(getString(R.string.car_brand_select_car_model));
        smartRefreshLayout.setOnLoadmoreListener(refreshlayout -> {
            if(carModelAdapter!=null&&carModelAdapter.getItemCount()>=total){
                smartRefreshLayout.finishLoadmore();
                smartRefreshLayout.setEnableLoadmore(false);
                return;
            }else {
                page++;
                onLoadRetry();
            }
        });
        return true;
    }


    @OnClick(R.id.iv_back)
    public void backClick() {
        NavHostFragment.findNavController(this).navigateUp();
    }

    OnClickListener<CarModel>onClickListener=carModel -> {
          carBrandViewModel.getSelectedCarModel().setValue(carModel);
          backClick();
        NavHostFragment.findNavController(this).popBackStack(R.id.carBrandFragment,true);
    };
}
